package day02;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import util.JdbcUtil;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Arrays;

@WebServlet("/add")
public class AddEmpServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("AddEmpServlet.service");

        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        PrintWriter out=response.getWriter();

        //获得表单数据
        String name=request.getParameter("name");
        String salaryStr=request.getParameter("salary");
        Double salary=salaryStr==null||"".equals(salaryStr.trim())
                        ?null
                        :Double.parseDouble(salaryStr);
        String sex=request.getParameter("sex");
        String eduStr=request.getParameter("edu");
        String edu="-1".equals(eduStr)
                        ?null
                        :eduStr;
        String[] hobbiesArr=request.getParameterValues("hobbies");
        String[] hobbies=hobbiesArr==null
                        ?new String[]{}
                        :hobbiesArr;
        String description=request.getParameter("description");


        System.out.println("name = " + name);
        System.out.println("salary = " + salary);
        System.out.println("sex = " + sex);
        System.out.println("edu = " + edu);
        System.out.println("hobbies = " + Arrays.toString(hobbies));
        System.out.println("description = " + description);

        //将数据保存到数据库
        Connection con=null;
        PreparedStatement ps=null;
        try{
            con= JdbcUtil.getConnection();
            con.setAutoCommit(false);

            String sql=new StringBuffer()
                    .append("insert into t_emp ")
                    .append("    (name,salary,sex,edu,hobbies,description) ")
                    .append("values ")
                    .append("    (?,?,?,?,?,?) ")
                    .toString();

            ps=con.prepareStatement(sql);

            ps.setString(1,name);
            ps.setDouble(2,salary);
            ps.setString(3,sex);
            ps.setString(4,edu);
            ps.setString(5, String.join(",", hobbies));
            ps.setString(6,description);

            ps.executeUpdate();


            con.commit();

//            out.println("<h1>添加成功</h1>");

            response.sendRedirect(request.getContextPath()+"/list");

        }catch (Exception e){
            e.printStackTrace();
            try {
                con.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            out.println("<h1>添加失败</h1>");
        }finally{
            JdbcUtil.close(null,ps,con);
        }



















    }
}
